package web;

import utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import static tools.CodeCg.ISOtoUTF8;

public class RegisterServlet extends BaseServlet{
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("register get");

        // 处理注册页面的请求


        // 修改编码格式
        req.setCharacterEncoding("UTF-8");		// 接受到的
//        resp.setContentType("text/html; charset=UTF-8");		// 修改发送的


        /*
         * 连接数据库步骤
         * 1. 加载驱动
         * 2. 获取连接对象 connection
         * 3. 编写sql语句
         * 4. 获取执行对象 preparedStatement
         * 5. 执行sql语句 如果sql语句是一个查询的话，使用的方法是executeQuery,如果sql语句是除了查询以外的话，使用的方法是executeUpdate
         */

        Connection connection = null;
        PreparedStatement pStatement = null;

        Map<String, String[]> map = req.getParameterMap();
//		 System.out.println(map);
        System.out.println(map.get("userNum")[0]);
        System.out.println();
        System.out.println(map);
        System.out.println(map.get("userName")[0]);
        String userName = ISOtoUTF8(req.getParameter("userName"));;
        String userAbl = "1";
        if("23177".equals(map.get("userAbl")[0]))  userAbl = "8";
//        req.getParameter("userName");
//        userName=new String(userName.getBytes("iso8859-1"),"UTF-8");
        try {
            connection = JDBCUtils.getConnection();


            // String sql = "insert into lin1(rolid,name,pwd,sex) values(?,?,?,?)";
//            String sql = "insert into student( userNum,userName,userSex,userFac,userMaj,userCla,  userBir,userTel,userAds,userPwd ) values(  ?,?,?,  ?,?,?,  ?,?,?,?)";
            String sql = "insert into student( userNum , userName , userPwd , userBir ,userAbl) value( ?,?,?,?,?)";
            pStatement = connection.prepareStatement(sql);
            // 与上sql一一对应
            pStatement.setString(1, map.get("userNum")[0]);
            pStatement.setString(2, userName);

            pStatement.setString(3, map.get("userPwd")[0]);

            pStatement.setString(4, map.get("userBir")[0]);
            pStatement.setString(5, userAbl);


            int count = pStatement.executeUpdate();
            // 判断
            if( count > 0) {
                System.out.println("注册成功");
                resp.sendRedirect("/webdemo2/views/loginDemo.html");	// 改变地址栏 重定向
//				 req.getRequestDispatcher("/").forward(req, resp);		// 不会改变地址栏，传递请求
            }
            else {
                System.out.println("注册失败");
//                resp.sendRedirect("/webdemo2/views/loginDemo.html");	// 改变地址栏 重定向
//                req.setAttribute("msg", "注册失败");
//                req.getRequestDispatcher("/Testfirst/views/fails.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("数据库插入失败");
            e.printStackTrace();
            req.setAttribute("msg", "注册失败");
//            req.getRequestDispatcher("/views/fails.jsp").forward(req, resp);
        } catch (NullPointerException e) {
            // TODO: handle exception
            System.out.println("信息不完整");
            req.setAttribute("msg", "注册失败,信息填写不完整");
//            req.getRequestDispatcher("/views/fails.jsp").forward(req, resp);
        } finally {
            JDBCUtils.close(connection, pStatement);
        }
    }
}
